/*
 * Copyright 1999-2022 Alibaba Group Holding Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.alibaba.nacos.plugin.datasource.impl.oracle;

import com.alibaba.nacos.common.utils.CollectionUtils;
import com.alibaba.nacos.plugin.datasource.constants.DataSourceConstant;
import com.alibaba.nacos.plugin.datasource.constants.FieldConstant;
import com.alibaba.nacos.plugin.datasource.mapper.HistoryConfigInfoMapper;
import com.alibaba.nacos.plugin.datasource.model.MapperContext;
import com.alibaba.nacos.plugin.datasource.model.MapperResult;

import java.sql.Timestamp;

/**
 * The mysql implementation of HistoryConfigInfoMapper.
 *
 * @author hyx
 **/

public class HistoryConfigInfoMapperByOracle extends AbstractMapperByOracle implements HistoryConfigInfoMapper {

    @Override
    public MapperResult removeConfigHistory(MapperContext context) {
        final Timestamp startTime = (Timestamp) context.getWhereParameter(FieldConstant.START_TIME);
        final Integer limitSize = (Integer) context.getWhereParameter(FieldConstant.LIMIT_SIZE);

        String sql = "DELETE FROM his_config_info WHERE (gmt_modified, id) IN (SELECT gmt_modified, "
                + "id FROM his_config_info WHERE gmt_modified < ? AND ROWNUM <= ? ORDER BY gmt_modified)";

        return new MapperResult(sql, CollectionUtils.list(startTime, limitSize));
    }

    @Override
    public MapperResult pageFindConfigHistoryFetchRows(MapperContext context) {
        // 使用 ROW_NUMBER() 进行分页
        String sql = "SELECT * FROM ( "
                + " SELECT nid, data_id, group_id, tenant_id, app_name, src_ip, src_user, op_type, gmt_create, gmt_modified, "
                + " ROW_NUMBER() OVER (ORDER BY nid DESC) AS row_num "
                + " FROM his_config_info "
                + " WHERE data_id = ? AND group_id = ? AND tenant_id = ? "
                + ") WHERE row_num > ? AND row_num <= ?";

        // 计算起始行和结束行
        int startRow = context.getStartRow();
        int pageSize = context.getPageSize();
        int endRow = startRow + pageSize;

        // 返回 MapperResult
        return new MapperResult(sql, CollectionUtils.list(
                context.getWhereParameter(FieldConstant.DATA_ID),
                context.getWhereParameter(FieldConstant.GROUP_ID),
                context.getWhereParameter(FieldConstant.TENANT_ID),
                startRow,
                endRow
        ));
    }

    @Override
    public String getDataSource() {
        return DataSourceConstant.ORACLE;
    }
}
